﻿--[[
   @brief    game_card_tips_model
   @author   hqzheng
   @version  1.0
   @date     2018-07-17
--]]

game_card_tips_model = 
{
    -- server通知类型
    MSG_MSGBOX_GAME_CARD_APPID = 50000011;
    MSG_MSGBOX_SYSTEM = 100001; -- 系统消息
    MSG_MSGBOX_PERSON = 100002; -- 普通消息
    MSG_MSGBOX_SUB_TYPE = 5;

    query_msg_url = LoadStr("GAME_CARD_TIPS_URL");

    cookie_info = {};

    self_tgp_id = nil;
    msg_list = nil;
    has_unread_msg = false;
    is_need_query_msg = false;
    has_try_to_query_msg = false;
    MAX_QUERY_MSG_COUNT = 20;

    query_game_card_msg = function (offset)
        -- 构造消息体
        local msg_tb = {};
        msg_tb["appid"] = game_card_tips_model.MSG_MSGBOX_GAME_CARD_APPID;-- GAME_ID.GAME_ID_SOCIAL; -- 统一分配
        msg_tb["msgtype"] = game_card_tips_model.MSG_MSGBOX_SYSTEM; --input.msg_type;
        msg_tb["offset"] = offset;
        msg_tb["count"] = game_card_tips_model.MAX_QUERY_MSG_COUNT;
        local sub_type_tb = {};
        table.insert(sub_type_tb, 5);
        msg_tb["msgsubtype"] = sub_type_tb;
        local query_msg_url = game_card_tips_model.query_msg_url;
        base_utility.curl_request_async(query_msg_url,
            "game_card_tips_model.on_query_msg_rsp", false, nil, true, nil,
             base_utility.table_to_json(msg_tb));
    end;

    -- 查询消息结果
    on_query_msg_rsp = function (result)
        if nil == result then
            base_utility.log(
                "[game_card_tips_model][on_query_msg_rsp] result is null");
            return;
        end
        local param = base_utility.json_to_table(result);
        if param ~= nil and type(param) == "table" then
            if 0 == param.ErrorCode and 
                nil ~= param.ErrorInfo and nil ~= param.data then
                base_utility.log(
                    string.format(
                    "[game_card_tips_model][on_query_msg_rsp]errorCode:%d, errMsg:%s",
                    tonumber(param.ErrorCode), tostring(param.ErrorInfo)), true);
                local msg_list = param.data.msg_list;
                if nil ~= msg_list then
                    if nil == game_card_tips_model.msg_list then
                        game_card_tips_model.msg_list = msg_list;
                    else
                        game_card_tips_model.attach_table(game_card_tips_model.msg_list, msg_list);
                    end
                    if #msg_list >= game_card_tips_model.MAX_QUERY_MSG_COUNT then
                        -- 超过20个则需要再拉一次
                        game_card_tips_model.is_need_query_msg = true;
                    else
                        game_card_tips_model.is_need_query_msg = false;
                    end
                    if false == game_card_tips_model.has_unread_msg then
                        for k, v in pairs(msg_list) do
                            if 0 == v.hasread then
                                game_card_tips_model.has_unread_msg = true;
                                break;
                            end
                        end;
                    end
                else
                    game_card_tips_model.is_need_query_msg = false;
                end
            end
        end

        if game_card_tips_model.is_need_query_msg then
             game_card_tips_model.query_game_card_msg(#game_card_tips_model.msg_list);
        else
            if game_card_tips_model.has_unread_msg then
                -- 弹tips
                game_card_tips_control.show_tips();
            end
        end;
    end;

    attach_table = function (source_tb, target_tb)
        if nil == target_tb or nil == source_tb then
            return;
        end

        for k, v in pairs(target_tb) do
            table.insert(source_tb, v);
        end
    end;
};



